<!DOCTYPE html>
<html>
<head>
	<meta charset="UTF-8">
	<title>随机取值</title>
    <link rel="stylesheet" type="text/css" href="../../style/public.css">
</head>
<body>
    <div id="section">
        <p>随机取值</p>
        <p>在2-10之间随机取一个值</p>
        <p id="num"></p>
        <p>在 ['red', 'blue', 'black', 'yellow', 'green'] 随机取一个值</p>
        <p id="color"></p>
    </div>
	
</body>
    <script>
    function selectFrom(lowerVal,upperValue){
        /*
        lowerVal:最小值
        upperValue:最大值
        */
        var choices = upperValue - lowerVal + 1;//取值个数
        return Math.floor(Math.random()*choices + lowerVal);//返回随机取值
    }

    var num = selectFrom(2,10);//在2-10之间随机取值;
    document.getElementById('num').innerHTML = num;

    var colors = ['red', 'blue', 'black', 'yellow', 'green'];
    var color = colors[selectFrom(0,colors.length-1)];
    document.getElementById('color').innerHTML = color;
    </script>
</html>